
package uestc.bitman.edxapp.mainui;

import uestc.bitman.edxapp.EdxApp;
import uestc.bitman.edxapp.R;
import uestc.bitman.edxapp.courselist.MyCourseListFragment;
import uestc.bitman.edxapp.courses.CoursesFragment;
import uestc.bitman.edxapp.downloaded.DownloadedFragment;
import uestc.bitman.edxapp.downloading.DownloadFragment;
import uestc.bitman.edxapp.downloading.DownloadingTaskAdapter;
import uestc.bitman.edxapp.misc.AboutFragment;

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * @category The main activty of this app.
 */
public class MainActivity extends PressAgainExitActivity implements OnItemClickListener {

    private MyCourseListFragment mCourseListFragment = null;

    private CoursesFragment mCoursesFragment = null;

    private DownloadFragment mDownloadFragment = null;

    private DownloadedFragment mDownloadedFragment = null;

    private AboutFragment mAboutFragment = null;

    private DrawerLayout mDrawerLayout;

    private ActionBarDrawerToggle mDrawerToggle;

    private ListView mListView;

    private DrawerAdapter mDrawerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initDrawer();

        EdxApp.setDownloadingTaskAdapter(new DownloadingTaskAdapter(this));

        if (savedInstanceState == null) {
            mCourseListFragment = new MyCourseListFragment();
            getFragmentManager().beginTransaction()
                    .replace(R.id.content_frame, mCourseListFragment).commit();
        }
    }

    @Override
    public void handleBackPress() {
        ActionBar actionBar = getActionBar();
        actionBar.removeAllTabs();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    }

    private void initDrawer() {

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mListView = (ListView) findViewById(R.id.left_drawer);
        mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        mDrawerAdapter = new DrawerAdapter(this);
        mListView.setAdapter(mDrawerAdapter);
        mListView.setOnItemClickListener(this);
        mDrawerLayout.setDrawerListener(setUpDrawerToggle());
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        // show the drawer automatically
        // mDrawerLayout.openDrawer(mListView);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {

        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        replaceCategoryFragment(position);
        mListView.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(mListView);
    }

    private void replaceCategoryFragment(int position) {

        setTitle(mDrawerAdapter.getItem(position));
        switch (position) {
            case 0:
                getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                if (mCoursesFragment == null) {
                    mCoursesFragment = new CoursesFragment();
                }
                switchFragment(mCoursesFragment, "courses", false, true);
                break;
            case 1:
                getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                if (mCourseListFragment == null) {
                    mCourseListFragment = new MyCourseListFragment();
                }
                switchFragment(mCourseListFragment, "courseList", false, true);
                break;
            case 2:
                if (mDownloadFragment == null) {
                    mDownloadFragment = new DownloadFragment();
                }
                switchFragment(mDownloadFragment, "download", true, false);
                break;
            case 3:
                if (mDownloadedFragment == null) {
                    mDownloadedFragment = new DownloadedFragment();
                }
                switchFragment(mDownloadedFragment, "downloaded", true, false);
                break;
            case 4:
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
                finish();
                break;
            case 5:
                if (mAboutFragment == null) {
                    mAboutFragment = new AboutFragment();
                }
                switchFragment(mAboutFragment, "about", true, false);
            default:
                break;
        }
    }

    private void switchFragment(Fragment fragment, String tagString, boolean addToBackStack,
            boolean pressAgainExit) {
        
        enablePressAgainToExit(pressAgainExit);
        startFragment(fragment, tagString, addToBackStack);
    }

    private DrawerListener setUpDrawerToggle() {

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer,
                R.string.drawer_open_title, R.string.drawer_close_title) {
            @Override
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                invalidateOptionsMenu();
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu();
            }
        };
        return mDrawerToggle;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // MenuInflater inflater = getMenuInflater();
        // mCurrentFragment.onCreateOptionsMenu(menu, inflater);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // boolean isDrawerOpen = drawerLayout.isDrawerOpen(listView);
        // menu.findItem(R.id.action_bitman).setVisible(!isDrawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    public void startFragment(Fragment fragment, String tagString, boolean addToBackStack) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.content_frame, fragment, tagString);
        if (addToBackStack) {
            transaction.addToBackStack(tagString);
        }
        transaction.commit();
    }
}
